package com.fishing.controller;


import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import QL.ButlerRealGoodsOrderGetListRequest;
import QL.ButlerRealGoodsOrderGetListResponse;
import QL.ButlerRealGoodsOrderSetStateRequest;
import QL.ButlerRealGoodsOrderSetStateResponse;
import QL.QLClient;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
/***
 * 兑换码
 * @author lrz
 *
 */
@Controller
public class RedeemCodeController{
	private static QLClient client = new QLClient();
	
	@RequestMapping("redeemCode/toList")
	public ModelAndView toList(HttpServletRequest request)throws Exception{
		ModelAndView mav = new ModelAndView("redeemCode/list");
		return mav;
	}
	
	@RequestMapping("redeemCode/detail/{type}")
	public ModelAndView detail(@PathVariable("type")String type, Long userId, Long srcUserId, Long toUserId, HttpServletRequest request)throws Exception{
		ModelAndView mav = new ModelAndView("redeemCode/detail_"+type);
		return mav;
	}
	
	@ResponseBody
	@RequestMapping(value="exchange/list",produces="text/html;charset=UTF-8")
	public com.alibaba.fastjson.JSONObject list(HttpServletRequest request,String OrderID,String UserId,String GoodsId,String PageIndex,String PageCount,String Statesend,String startRedeemTime,String endRedeemTime,String startHandleTime,String endHandleTime)throws Exception{
		Date  date1 = new Date();
		SimpleDateFormat date2 = new SimpleDateFormat ("yyyy-MM-dd");
		ButlerRealGoodsOrderGetListRequest req = new ButlerRealGoodsOrderGetListRequest();
		ButlerRealGoodsOrderGetListResponse rsp = new ButlerRealGoodsOrderGetListResponse();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		long count = 0;
		if(OrderID == "") {
			OrderID = "0";
			req.OrderId = Integer.parseInt(OrderID);
		}
		if(UserId == "") {
			UserId = "0";
			req.OrderId = Integer.parseInt(UserId);
		}
		if(GoodsId == "") {
			GoodsId = "0";
			req.OrderId = Integer.parseInt(GoodsId);
		}
		if ((("").equals(startRedeemTime) && ("").equals(endRedeemTime)) && (("").equals(startHandleTime) && ("").equals(endHandleTime))) {
			startRedeemTime = date2.format(date1);
			endRedeemTime = date2.format(date1.getTime() + 60*60*1000*24);
			startHandleTime = date2.format(date1);
			endHandleTime = date2.format(date1.getTime() + 60*60*1000*24);
			req.IsFilterProcessTime = 0;
			req.IsFilterCreateTime = 0;
		}else if (!(("").equals(startRedeemTime) && ("").equals(endRedeemTime)) && (("").equals(startHandleTime) && ("").equals(endHandleTime))) {
			startHandleTime = "";
			endHandleTime = "";
			req.IsFilterProcessTime = 0;
			req.IsFilterCreateTime = 1;
		}else if ((("").equals(startRedeemTime) && ("").equals(endRedeemTime)) && !(("").equals(startHandleTime) && ("").equals(endHandleTime))) {
			startRedeemTime = "";
			endRedeemTime = "";
			req.IsFilterProcessTime = 1;
			req.IsFilterCreateTime = 0;
		}else {
			req.IsFilterProcessTime = 1;
			req.IsFilterCreateTime = 1;
		}
		req.OrderId = Integer.parseInt(OrderID);
		req.UserId  = Integer.parseInt(UserId);
		req.GoodsId = Integer.parseInt(GoodsId);
		req.State = Integer.parseInt(Statesend);
		req.StartCreateTime = startRedeemTime;//开始兑换时间
		req.EndCreateTime = endRedeemTime;
		req.StartProcessTime = startHandleTime;
		req.EndProcessTime = endHandleTime;
		req.PageIndex = Integer.parseInt(PageIndex)-1;
		req.PageCount = Integer.parseInt(PageCount);
		if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		for (int i = 0;i < rsp.InfoList.size();i++) {
			rsp.InfoList.get(i).TotalRecharge = rsp.InfoList.get(i).TotalRecharge/100;
		}
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("data", rsp.InfoList);
		resultMap.put("count", rsp.TotalCount);
		resultMap.put("message", "");
		resultMap.put("status", "200");
		com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject(resultMap);
		return json;
	}
	
	@ResponseBody
	@RequestMapping(value = "redeemCode/select",produces="text/html;charset=UTF-8")
	public String select(HttpServletRequest request,String OrderId,String State) {
		ButlerRealGoodsOrderSetStateRequest req = new ButlerRealGoodsOrderSetStateRequest();
		req.ZoneId = (Integer)request.getSession().getAttribute("ZoneId");
		req.OrderId = Integer.parseInt(OrderId);
		req.State = Integer.parseInt(State);
		req.Operator = (String) request.getSession().getAttribute("Admintruename");
		ButlerRealGoodsOrderSetStateResponse rsp = new  ButlerRealGoodsOrderSetStateResponse();
		if (req.ZoneId < 0)
			client.ServerUrl = "http://api2.qq1798.com:8000/router/rest";
		client.Execute(req, rsp);
		return rsp.ErrMsg;
	}
}
